package modulesAPI.v2.composite;
import java.util.Calendar;
import java.util.GregorianCalendar;
import com.caterina.components.CPanel;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;

import event.AcceptanceState;
import event.Event;
import event.Event.EventType;
import event.Event.PreparationType;
import event.EventState;
import model.Model;
import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.EmptyModule;
import modulesAPI.v2.basics.ModCalendar;
import modulesAPI.v2.basics.ModComboBox;
import modulesAPI.v2.basics.ModIntTextField;
import modulesAPI.v2.basics.ModTextField;

public class MEvent extends Module<Event>{
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un evento
	 * */
	public MEvent(Module<?> ownerModule, Event content, String tag,String description, ViewType viewType,int...options) {
		super(ownerModule, content, tag, description, viewType,options);
		if (content!=null){
			this.createModules();
			this.addModules(eventName,eventType,preparationType,serviceType,beginDate,endDate,place,guestsNumber);
		}else
			this.addModules(new EmptyModule());
	}
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un evento
	 * */
	public MEvent(Module<?> ownerModule, String tag,String description, ViewType viewType) {
		this(ownerModule, 
				new Event(Model.NO_ID, "", EventType.BIRTHDAY, PreparationType.STANDARD,Event.ServiceType.TABLE, new GregorianCalendar(), new GregorianCalendar(), "", 0, new AcceptanceState()),
				tag, description, viewType);
	}
	private void createModules(){
		if (this.isOptionEnabled(VIEW_LESS))this.createViewLessModules();
		else if(this.isOptionEnabled(VIEW_MORE_LESS))this.createViewMoreLessModules();
		else this.createStandardView();
	}
	private void createStandardView(){
		this.eventName=new ModTextField(this, this.getContent().getEventName(), "EVENT_NAME", "EVENT_NAME_DESC", this.getViewType());
		this.place=new ModTextField(this, this.getContent().getPlace(), "EVENT_PLACE", "EVENT_PLACE_DESC", this.getViewType());
		this.guestsNumber=new ModIntTextField(this, this.getContent().getGuestsNumber(), "EVENT_GUESTS_NUMBER", "EVENT_GUESTS_NUMBER_DESC", this.getViewType());
		this.eventType=new ModComboBox(this, this.getContent().getEventType(), "EVENT_TYPE", "EVENT_TYPE_DESC", this.getViewType(), EventType.values());
		this.preparationType=new ModComboBox(this, this.getContent().getPreparationType(), "EVENT_PREPARATION_TYPE", "EVENT_PREPARATION_TYPE_DESC", this.getViewType(),PreparationType.values());
		this.serviceType=new ModComboBox(this, this.getContent().getServiceType(), "EVENT_SERVICE_TYPE", "EVENT_SERVICE_TYPE_DESC", this.getViewType(),Event.ServiceType.values());
		this.beginDate=new ModCalendar(this, this.getContent().getBeginDate(), "EVENT_BEGIN_DATE", "EVENT_BEGIN_DATE_DESC", this.getViewType());
		this.endDate=new ModCalendar(this, this.getContent().getEndDate(), "EVENT_END_DATE", "EVENT_END_DATE_DESC", this.getViewType());
		this.state = new MEventState(this, this.getContent().getEventState(),"EVENT_STATE", "EVENT_STATE_DESC", this.getViewType());
	}
	private void createViewLessModules(){
		this.eventName=new ModTextField(this, this.getContent().getEventName(), "EVENT_NAME","EVENT_NAME_DESC", this.getViewType());
		this.place=new ModTextField(this, this.getContent().getPlace(), "EVENT_PLACE", "EVENT_PLACE_DESC", this.getViewType());
		this.guestsNumber=new ModIntTextField(this, this.getContent().getGuestsNumber(), "EVENT_GUESTS_NUMBER", "EVENT_GUESTS_NUMBER_DESC",this.getViewType());
		this.eventType=new ModComboBox(this, this.getContent().getEventType(), "EVENT_TYPE", "EVENT_TYPE_DESC", this.getViewType(), EventType.values());
		this.preparationType=new ModComboBox(this, this.getContent().getPreparationType(), "EVENT_PREPARATION_TYPE", "EVENT_PREPARATION_TYPE_DESC", this.getViewType(),PreparationType.values());
		this.serviceType=new ModComboBox(this, this.getContent().getServiceType(), "EVENT_SERVICE_TYPE","EVENT_SERVICE_TYPE_DESC", this.getViewType(),Event.ServiceType.values());
		this.beginDate=new ModCalendar(this, this.getContent().getBeginDate(), "EVENT_BEGIN_DATE", "EVENT_BEGIN_DATE_DESC", this.getViewType());
		this.endDate=new ModCalendar(this, this.getContent().getEndDate(), "EVENT_END_DATE", "EVENT_END_DATE_DESC", this.getViewType());
		this.state = new MEventState(this, this.getContent().getEventState(), "EVENT_STATE", "EVENT_STATE_DESC", this.getViewType());
	}
	private void createViewMoreLessModules(){
		this.eventName=new ModTextField(this, this.getContent().getEventName(),"EVENT_NAME", "EVENT_NAME_DESC", this.getViewType());
		this.place=new ModTextField(this, this.getContent().getPlace(), "EVENT_PLACE", "EVENT_PLACE_DESC", this.getViewType());
		this.guestsNumber=new ModIntTextField(this, this.getContent().getGuestsNumber(), "EVENT_GUESTS_NUMBER", "EVENT_GUESTS_NUMBER_DESC", this.getViewType());
		this.eventType=new ModComboBox(this, this.getContent().getEventType(), "EVENT_TYPE", "EVENT_TYPE_DESC", this.getViewType(), ViewType.HIDDEN);
		this.preparationType=new ModComboBox(this, this.getContent().getPreparationType(), "EVENT_PREPARATION_TYPE", "EVENT_PREPARATION_TYPE_DESC", this.getViewType(),PreparationType.values());
		this.serviceType=new ModComboBox(this, this.getContent().getServiceType(), "EVENT_SERVICE_TYPE", "EVENT_SERVICE_TYPE_DESC", ViewType.HIDDEN,Event.ServiceType.values());
		this.beginDate=new ModCalendar(this, this.getContent().getBeginDate(), "EVENT_BEGIN_DATE", "EVENT_BEGIN_DATE_DESC", this.getViewType());
		this.endDate=new ModCalendar(this, this.getContent().getEndDate(), "EVENT_END_DATE","EVENT_END_DATE_DESC", this.getViewType());
		this.state = new MEventState(this, this.getContent().getEventState(),"EVENT_STATE", "EVENT_STATE_DESC", ViewType.HIDDEN);
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		return CModules.formatForList(this.getContent().getEventName(), CWords.getWord(this.getContent().getEventType().name())+" "+CWords.getWord(this.getContent().getEventState().getName()), isSelected);
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return null;
	}
	protected void beforeFireChange(Module<?>mod){
		int guests=guestsNumber.getContent();
		this.setContent(new Event(
				this.getContent().getId(), 
				eventName.getContent(), 
				(EventType)eventType.getContent(), 
				(PreparationType)preparationType.getContent(),
				(Event.ServiceType)this.serviceType.getContent(),
				beginDate.getContent(), 
				endDate.getContent(), 
				place.getContent(), 
				guests, 
				this.getContent().getEventState())
		); 	

	}
	
	@Override
	protected void afterFireChange(Module<?> mod) {
		
	}
	public static final int VIEW_LESS=0;
	public static final int VIEW_MORE_LESS=1;
	private Module<String> eventName;
	private Module<String> place;
	private Module<Integer> guestsNumber;
	private Module<Enum<?>> eventType;
	private Module<Enum<?>> serviceType;
	private Module<Enum<?>> preparationType;
	private Module<Calendar> beginDate;
	private Module<Calendar> endDate;
	@SuppressWarnings("unused")
	private Module<EventState> state;
}
